package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.dubbo.api.QuestionsApi;
import com.tanhua.dubbo.api.ResultApi;
import com.tanhua.dubbo.api.TestSoulApi;
import com.tanhua.model.domain.Questions;
import com.tanhua.model.domain.Result;
import com.tanhua.model.domain.TestSoul;
import com.tanhua.model.vo.TestSoulVo;
import com.tanhua.server.interceptor.RandomById;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TestSoulService {

    @DubboReference
    private TestSoulApi testSoulApi;

    @DubboReference
    private QuestionsApi questionsApi;

    @DubboReference
    private ResultApi resultApi;

    public List<TestSoulVo> questionList() {
        List<TestSoul> list = testSoulApi.findAll();
        int count = questionsApi.count();
        Result result = resultApi.selectByUserId(UserHolder.getUserId());

        List<TestSoulVo> vos = new ArrayList<>();
        for (TestSoul testSoul : list) {
            TestSoulVo vo = TestSoulVo.init(testSoul);
            List random = RandomById.random(count, vo.getNum());
            List<Questions> questions =questionsApi.findByIds(random);
            List<Questions> questionsList = Questions.questions(questions);
            vo.setQuestions(questionsList);
            if(result != null){
                vo.setReportId(result.getId().toString());
            }
            vos.add(vo);
        }
        return vos;
    }
}
